package com.gisterrier.client.components;

import java.util.ArrayList;

import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.Pixel;
import org.gwtopenmaps.openlayers.client.Style;
import org.gwtopenmaps.openlayers.client.event.EventHandler;
import org.gwtopenmaps.openlayers.client.event.EventObject;
import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.format.WKT;
import org.gwtopenmaps.openlayers.client.layer.Vector;
import org.gwtopenmaps.openlayers.client.util.JSObject;

import com.common.client.map.GisMapEvent;
import com.common.client.map.GisMapEventHandler;
import com.common.client.map.MapViewerPanel;
import com.common.shared.map.GisLayer;
import com.common.shared.map.GisLayerOptions;
import com.common.shared.map.GisMap;
import com.common.shared.map.GisMapClickedObject;
import com.common.shared.map.GisMapProperties;
import com.gisterrier.client.GisTerrier;
import com.gisterrier.shared.GissTigerConstants;
import com.gisterrier.shared.Map_Customer_to_Building;
import com.gisterrier.shared.Map_Customer_to_Building_Obj;
import com.gisterrier.shared.Map_Street_to_Street;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class MainPanel extends HLayout implements GisMapEventHandler {
	private POptions pOptions;
	public PSearchContainer searchContainer;
	private PMapObjects mapObjects;
	public static MainPanel mainPanel;
	private Map map;
	MapViewerPanel mapPanel;
	private Vector vec;

	public MainPanel() {
		mainPanel = this;
		this.setWidth("99%");
		this.setHeight("98%");
		pOptions = new POptions();
		this.addMember(pOptions);
		GisMap gisMap = createGisMap();

		VLayout hlmapObjects = new VLayout();
		hlmapObjects.setShowResizeBar(true);
		mapPanel = new MapViewerPanel(gisMap);
		HLayout hlmapContainer = new HLayout();
		hlmapContainer.addMember(mapPanel);
		hlmapContainer.setShowResizeBar(true);
		hlmapContainer.setHeight("60%");
		hlmapObjects.addMember(hlmapContainer);
		mapObjects = new PMapObjects();
		mapObjects.setWidth100();
		mapObjects.setHeight("40%");
		hlmapObjects.addMember(mapObjects);
		// PUserManager vl = new PUserManager();
		// vl.setID("UserManager");
		this.addMember(hlmapObjects);
		searchContainer = new PSearchContainer();
		// searchContainer.setShowResizeBar(true);
		this.addMember(searchContainer);
		mapPanel.addGisMapEventHandler(this);

		vec = new Vector("Selected Shape");

	}

	public int getSrid(String srtSrid) {
		return Integer
				.parseInt(srtSrid.substring(srtSrid.indexOf((char) ':') + 1));
	}

	public int getSrid(Map map) {
		return getSrid(map.getProjection());
	}

	private void zoom(GisMapClickedObject result) {
		if (result == null)
			return;
		vec.destroyFeatures();
		VectorFeature[] ft = selectShape(map, vec, result.getGis_astext());
		for (VectorFeature vectorFeature : ft) {
			map.zoomToExtent(vectorFeature.getGeometry().getBounds());
			break;
		}
	}

	public void zoomToBuilding(long building_id) {
		GisTerrier.gissTerrierService.getBuilding(building_id,
				new AsyncCallback<GisMapClickedObject>() {

					@Override
					public void onSuccess(GisMapClickedObject result) {
						zoom(result);

					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	private VectorFeature[] selectShape(Map map, Vector vec, String geom_text) {
		WKT wkt = new WKT();
		VectorFeature[] ft = wkt.read(geom_text);
		Style st = new Style();
		st.setFillColor("#FF0044");
		st.setStrokeColor("black");
		st.setStrokeWidth(5);
		for (VectorFeature vectorFeature : ft) {
			vectorFeature.setStyle(st);
		}

		vec.addFeatures(ft);
		return ft;

	}

	private void setClickedObject(Map map, Vector vec,
			ArrayList<GisMapClickedObject> arr) {
		vec.destroyFeatures();
		if (arr == null) {
			return;
		}
		for (GisMapClickedObject gisMapClickedObject : arr) {
			selectShape(map, vec, gisMapClickedObject.getGis_astext());
		}
		mapObjects.setMapClickedObjects(arr);

		// if (obj.getObj_type() == GissTigerConstants.MOT_STREET) {
		// GisTerrier.gissTerrierService.getMap_Street_to_Street(obj.getGid(),
		// true, new AsyncCallback<Map_Street_to_Street>() {
		//
		// @Override
		// public void onSuccess(Map_Street_to_Street result) {
		// System.out.println(result);
		//
		// }
		//
		// @Override
		// public void onFailure(Throwable caught) {
		// SC.say(caught.getMessage());
		//
		// }
		// });
		// }
		// if (obj.getObj_type() == GissTigerConstants.MOT_BUILDING) {
		// GisTerrier.gissTerrierService.getMap_Customer_to_Building(
		// obj.getGid(), true,
		// new AsyncCallback<Map_Customer_to_Building_Obj>() {
		//
		// @Override
		// public void onSuccess(
		// Map_Customer_to_Building_Obj result) {
		// System.out.println(result);
		//
		// }
		//
		// @Override
		// public void onFailure(Throwable caught) {
		// SC.say(caught.getMessage());
		//
		// }
		// });

	}

	private GisMap createGisMap() {
		GisMap gisMap = new GisMap();
		gisMap.setHeight("650");
		gisMap.setWidth("950");
		gisMap.setId(1);
		gisMap.setMapname("Tbilisi");
		gisMap.setZoomto("78592.286,4534755.316,707025.536,4848971.941");
		ArrayList<GisMapProperties> mapProperties = new ArrayList<GisMapProperties>();
		gisMap.setGisMaProperties(mapProperties);

		GisMapProperties gmP = new GisMapProperties();
		mapProperties.add(gmP);
		gmP.setMap(gisMap);
		gmP.setPropertie_name("resolutions");
		gmP.setPropertie_value("1227.40869140625, 613.704345703125, 306.8521728515625, 153.42608642578125, 76.71304321289062, 38.35652160644531, 19.178260803222656, 9.589130401611328, 4.794565200805664, 2.397282600402832, 1.198641300201416, 0.599320650100708, 0.299660325050354, 0.149830162525177, 0.0749150812625885, 0.03745754063129425, 0.018728770315647125, 0.009364385157823563, 0.004682192578911781, 0.0023410962894558907, 0.0011705481447279453");
		gmP.setPtype(9);

		gmP = new GisMapProperties();
		mapProperties.add(gmP);
		gmP.setMap(gisMap);
		gmP.setPropertie_name("units");
		gmP.setPropertie_value("degrees");

		gmP = new GisMapProperties();
		mapProperties.add(gmP);
		gmP.setMap(gisMap);
		gmP.setPropertie_name("projection");
		gmP.setPropertie_value("EPSG:32638");

		gmP = new GisMapProperties();
		mapProperties.add(gmP);
		gmP.setMap(gisMap);
		gmP.setPropertie_name("displayProjection");
		gmP.setPropertie_value("EPSG:32638");

		gmP = new GisMapProperties();
		mapProperties.add(gmP);
		gmP.setMap(gisMap);
		gmP.setPropertie_name("maxExtent");
		gmP.setPropertie_value("78592.286,4534755.316,707025.536,4848971.941");
		gmP.setPtype(12);

		ArrayList<GisLayer> gisLayers = new ArrayList<GisLayer>();

		gisMap.setGisLayers(gisLayers);

		GisLayer gisLayer = new GisLayer();
		gisLayers.add(gisLayer);
		gisLayer.setBaselayer(true);
		gisLayer.setGmaptype(1);
		gisLayer.setIswms(true);
		gisLayer.setLayername("GeorgiaTiles");
		gisLayer.setPriority(1);
		gisLayer.setTitle("Georgia");
		gisLayer.setWmsurl("http://localhost:8080/GeoRouting/gwc/service/wms");

		ArrayList<GisLayerOptions> gisLayerOptions = new ArrayList<GisLayerOptions>();
		gisLayer.setGisLayerOptions(gisLayerOptions);

		GisLayerOptions lo = new GisLayerOptions();
		gisLayerOptions.add(lo);
		lo.setPropertie_name("layers");
		lo.setPropertie_value("GeorgiaGazTiled");

		lo = new GisLayerOptions();
		gisLayerOptions.add(lo);
		lo.setPropertie_name("format");
		lo.setPropertie_value("image/png");
		lo.setPtype(1);

		lo = new GisLayerOptions();
		gisLayerOptions.add(lo);
		lo.setPropertie_name("transparent");
		lo.setPropertie_value("true");
		lo.setPtype(6);

		lo = new GisLayerOptions();
		gisLayerOptions.add(lo);
		lo.setPropertie_name("tileSize");
		lo.setPropertie_value("256,256");
		lo.setPtype(1);

		lo = new GisLayerOptions();
		gisLayerOptions.add(lo);
		lo.setPropertie_name("tiled");
		lo.setPropertie_value("true");
		lo.setPtype(6);

		// gisLayer = new GisLayer();
		// gisLayers.add(gisLayer);
		// gisLayer.setBaselayer(false);
		// gisLayer.setGmaptype(1);
		// gisLayer.setIswms(true);
		// gisLayer.setLayername("SocarGazTiled");
		// gisLayer.setPriority(2);
		// gisLayer.setTitle("SocarGas");
		// gisLayer.setWmsurl("http://localhost:8080/GeoRouting/gwc/service/wms");
		//
		// gisLayerOptions = new ArrayList<GisLayerOptions>();
		// gisLayer.setGisLayerOptions(gisLayerOptions);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("layers");
		// lo.setPropertie_value("SocarGazTiled");
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("format");
		// lo.setPropertie_value("image/png");
		// lo.setPtype(1);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("transparent");
		// lo.setPropertie_value("true");
		// lo.setPtype(6);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("tileSize");
		// lo.setPropertie_value("256,256");
		// lo.setPtype(1);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("tiled");
		// lo.setPropertie_value("true");
		// lo.setPtype(6);

		// GisMap gisMap = new GisMap();
		// gisMap.setHeight("750");
		// gisMap.setWidth("1050");
		// gisMap.setId(1);
		// gisMap.setMapname("Tbilisi");
		// gisMap.setZoomto("4964308.762335241,5104237.505256674,5011349.01655737,5137656.446973733");
		// ArrayList<GisMapProperties> mapProperties = new
		// ArrayList<GisMapProperties>();
		// gisMap.setGisMaProperties(mapProperties);
		//
		// GisMapProperties gmP = new GisMapProperties();
		// mapProperties.add(gmP);
		// gmP.setMap(gisMap);
		// gmP.setPropertie_name("resolutions");
		// gmP.setPropertie_value("156543.03390625, 78271.516953125, 39135.7584765625, 19567.87923828125, 9783.939619140625, 4891.9698095703125, 2445.9849047851562, 1222.9924523925781, 611.4962261962891, 305.74811309814453, 152.87405654907226, 76.43702827453613, 38.218514137268066, 19.109257068634033, 9.554628534317017, 4.777314267158508, 2.388657133579254, 1.194328566789627, 0.5971642833948135, 0.29858214169740677, 0.14929107084870338, 0.07464553542435169, 0.037322767712175846, 0.018661383856087923, 0.009330691928043961, 0.004665345964021981");
		// gmP.setPtype(9);
		//
		// gmP = new GisMapProperties();
		// mapProperties.add(gmP);
		// gmP.setMap(gisMap);
		// gmP.setPropertie_name("units");
		// gmP.setPropertie_value("meters");
		//
		// gmP = new GisMapProperties();
		// mapProperties.add(gmP);
		// gmP.setMap(gisMap);
		// gmP.setPropertie_name("projection");
		// gmP.setPropertie_value("EPSG:900913");
		//
		// gmP = new GisMapProperties();
		// mapProperties.add(gmP);
		// gmP.setMap(gisMap);
		// gmP.setPropertie_name("displayProjection");
		// gmP.setPropertie_value("EPSG:4326");
		//
		// gmP = new GisMapProperties();
		// mapProperties.add(gmP);
		// gmP.setMap(gisMap);
		// gmP.setPropertie_name("maxExtent");
		// gmP.setPropertie_value("-2.003750834E7,-2.003750834E7,2.003750834E7,2.003750834E7");
		// gmP.setPtype(12);
		//
		// ArrayList<GisLayer> gisLayers = new ArrayList<GisLayer>();
		//
		// gisMap.setGisLayers(gisLayers);
		//
		// GisLayer gisLayer = new GisLayer();
		// gisLayers.add(gisLayer);
		// gisLayer.setBaselayer(true);
		// gisLayer.setGmaptype(1);
		// gisLayer.setIswms(true);
		// gisLayer.setLayername("Tbilisi");
		// gisLayer.setPriority(1);
		// gisLayer.setTitle("Tbilisi");
		// gisLayer.setWmsurl("http://homepc.homelinux.org:8080/GeoRouting/gwc/service/wms");
		//
		// ArrayList<GisLayerOptions> gisLayerOptions = new
		// ArrayList<GisLayerOptions>();
		// gisLayer.setGisLayerOptions(gisLayerOptions);
		//
		// GisLayerOptions lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("layers");
		// lo.setPropertie_value("TbilisiTile");
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("format");
		// lo.setPropertie_value("image/png");
		// lo.setPtype(1);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("transparent");
		// lo.setPropertie_value("true");
		// lo.setPtype(6);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("tileSize");
		// lo.setPropertie_value("256,256");
		// lo.setPtype(1);
		//
		// lo = new GisLayerOptions();
		// gisLayerOptions.add(lo);
		// lo.setPropertie_name("tiled");
		// lo.setPropertie_value("true");
		// lo.setPtype(6);

		return gisMap;
	}

	@Override
	public void onHandle(GisMapEvent eventObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void creationComplete() {
		map = mapPanel.getMap();
		map.addLayer(vec);
		map.getEvents().register("click", map, new EventHandler() {

			@Override
			public void onHandle(EventObject eventObject) {
				JSObject obj = eventObject.getJSObject();
				Pixel xy = Pixel.narrowToPixel(obj.getProperty("xy"));
				final LonLat addLonLat = map.getLonLatFromPixel(xy);
				Map_Customer_to_Building custBuild = new Map_Customer_to_Building();
				custBuild.setBuilding_id(1);
				custBuild.setCus_id(1);
				// GisTerrier.gissTerrierService.insertMap_Customer_to_Building(
				// custBuild, new AsyncCallback<Long>() {
				//
				// @Override
				// public void onSuccess(Long result) {
				// System.out.println(result);
				//
				// }
				//
				// @Override
				// public void onFailure(Throwable caught) {
				// // TODO Auto-generated method stub
				//
				// }
				// });

				GisTerrier.gissTerrierService.getGisMapClickedObjects(
						addLonLat.lon(), addLonLat.lat(), getSrid(map),
						new AsyncCallback<ArrayList<GisMapClickedObject>>() {

							@Override
							public void onSuccess(
									ArrayList<GisMapClickedObject> result) {
								setClickedObject(map, vec, result);
							}

							@Override
							public void onFailure(Throwable caught) {
								SC.say(caught.getMessage());
							}
						});
			}
		});

	}
}
